//
//  ViewController.m
//  加速计
//
//  Created by MyMac on 15/8/10.
//  Copyright (c) 2015年 MyMac. All rights reserved.
//

#import "ViewController.h"
#import <CoreMotion/CoreMotion.h>
@interface ViewController ()<UIAccelerometerDelegate>
@property (weak, nonatomic) IBOutlet UIButton *motionBtn;
@property(nonatomic,strong)CMMotionManager*motionManager;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    UIAccelerometer*accele=[UIAccelerometer sharedAccelerometer];
//    accele.delegate=self;
//    accele.updateInterval=0.1f;
//    self.motionBtn.layer.anchorPoint=CGPointMake(0.5, 0.5);
//    
    
    CMMotionManager*manager=[[CMMotionManager alloc]init];
    self.motionManager=manager;
    //加速度,,,即各个方向的重力加速度,与各轴的正方向相反，则加速度为负
//    if(manager.accelerometerAvailable){
//        manager.accelerometerUpdateInterval=0.1;
//        [manager startAccelerometerUpdatesToQueue:[[NSOperationQueue alloc]init] withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
//            if(error){
//                [manager stopAccelerometerUpdates];
//            
//            }else{
//            self.motionBtn.frame=CGRectMake(accelerometerData.acceleration.x, accelerometerData.acceleration.y,self.motionBtn.bounds.size.width,self.motionBtn.bounds.size.height);
//                NSLog(@"%f***%f***%f",accelerometerData.acceleration.x,accelerometerData.acceleration.y,accelerometerData.acceleration.z);
//            }
// 
//        }];
//    }
    
    //陀螺仪，，，旋转速度
//    if(manager.gyroAvailable){
//        manager.gyroUpdateInterval=0.1f;
//        [manager startGyroUpdatesToQueue:[[NSOperationQueue alloc]init] withHandler:^(CMGyroData *gyroData, NSError *error) {
//            if(error){
//                [manager stopGyroUpdates];
//            
//            
//            
//            }else {
//                NSLog(@"陀螺仪%f**%f*8%f",gyroData.rotationRate.x,gyroData.rotationRate.y,gyroData.rotationRate.z);
//            
//            
//            
//            }
//        }];
//    }
    //磁场
//    if(manager.magnetometerAvailable){
//        manager.magnetometerUpdateInterval=0.1;
//    [manager startMagnetometerUpdatesToQueue:[[NSOperationQueue alloc]init] withHandler:^(CMMagnetometerData *magnetometerData, NSError *error) {
//        if(error){
//            [manager stopMagnetometerUpdates];
//        
//        }else{
//        
//            NSLog(@"磁场%f****%f***%f",magnetometerData.magneticField.x,magnetometerData.magneticField.y,magnetometerData.magneticField.z);
//        
//        }
//    }];
//    }
    //手动调用磁场数据
    if( self.motionManager.magnetometerAvailable){
        [ self.motionManager startMagnetometerUpdates];
      
    }
    if(self.motionManager.magnetometerAvailable){
      NSLog(@"磁场%f****%f***%f",self.motionManager.magnetometerData.magneticField.x,self.motionManager.magnetometerData.magneticField.y,self.motionManager.magnetometerData.magneticField.z);
    
    }
    
}
//- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
//{
//
//    self.motionBtn.frame=CGRectMake(acceleration.x, acceleration.y,self.motionBtn.bounds.size.width,self.motionBtn.bounds.size.height);
////    NSLog(@"%@^^^%@",acceleration,accelerometer);
//    NSLog(@"%f***%f***%f",acceleration.x,acceleration.y,acceleration.z);
//
//
//
//
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
